package ua.kharkov.knure.mikhaylov.st4.web.command;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import ua.kharkov.knure.mikhaylov.st4.db.entities.Account;
import ua.kharkov.knure.mikhaylov.st4.db.enums.AccountState;
import ua.kharkov.knure.mikhaylov.st4.db.management.DBManager;
import ua.kharkov.knure.mikhaylov.st4.settings.LocaleData;
import ua.kharkov.knure.mikhaylov.st4.settings.Path;

/**
 * Command - block account
 * 
 * @author Saint
 * 
 */
public class BlockAccountCommand extends Command {

	private static final long serialVersionUID = 3591027779562834678L;
	private static final Logger log = Logger
			.getLogger(BlockAccountCommand.class);

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		log.debug("Command starts");

		log.trace("Checking parameters");

		Long id = Long.valueOf(request.getParameter("id"));

		DBManager.getInstance().updateAccountState(AccountState.CLOSED, id);

		Account account = (Account) request.getSession()
				.getAttribute("account");
		account.setAccountState(AccountState.CLOSED);
		request.getSession().setAttribute("account", account);

		request.setAttribute("message", LocaleData.getResourceBundle()
				.getString("servlet.success.account.block"));
		request.setAttribute("next", Path.COMMAND_VIEW_ACCOUNT);
		String forward = Path.PAGE_SUCCESS;
		log.debug("Command finished");
		return forward;
	}

}
